package com.loanapp.server.market.provider.modular.count.impl;

import com.loanapp.base.common.utils.StringUtil;
import com.loanapp.base.database.common.base.SqlMapper;
import com.loanapp.server.market.api.customer.entity.CustomPlatformCountInfo;
import com.loanapp.server.market.provider.modular.count.ICountServiceTwo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by yuzhengxing on 2017/9/1.
 */
@Service
public class CountServiceTwo implements ICountServiceTwo {

    @Autowired
    private SqlMapper sqlMapper;

    /*
     * 查询是id是否存在，是否是新的一天的开始
     */
    @Override
    public boolean isExist(String platformId,String buttonId,String starPlatformId,String bannerId,String homePageId, Date date) {
       CustomPlatformCountInfo cpc = selectCustomPlatformCountInfo(platformId,buttonId,starPlatformId,bannerId,homePageId,date);
       boolean b = false;
           if (cpc.getNum() != 0){
               b = true;//更新
           }
       //    log.info("cpc.getNum:"+cpc.getNum());


        return b;//新增
    }



    /*
     * 根据条件查询表
     * date表示是当天日期
     */
    @Override
    public CustomPlatformCountInfo selectCustomPlatformCountInfo(String platformId, String buttonId, String starPlatformId, String bannerId, String homePageId, Date date){

        //Date date=new Date();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        String updateDay=sdf.format(date);
        StringBuffer sql=new StringBuffer();
        sql.append("select count(1) num, id, platform_id platformId,home_page_id homePageId ,star_platform_id stratPlatfromId,banner_id bannerId,button_id buttonId,visit_count visitCount,click_count clickCount from customer_platform_count_info  " +
                "where 1=1 and update_time between CONCAT('" + updateDay + ", 00:00:00') and CONCAT('" + updateDay
                + ", 23:59:59')");
        if(StringUtil.isNotEmpty(platformId)){
            sql.append(" and platform_id="+platformId);
         //   log.info("platformId:"+platformId);
        }
        if(StringUtil.isNotEmpty(starPlatformId)){
            sql.append(" and star_platform_id="+starPlatformId);
        }
        if(StringUtil.isNotEmpty(bannerId)){
            sql.append(" and banner_id="+bannerId);
        }
        if(StringUtil.isNotEmpty(buttonId)){
            sql.append(" and button_id="+buttonId);
        }
        if(StringUtil.isNotEmpty(homePageId)){
            sql.append(" and home_page_id="+homePageId);
      //      log.info("homePageId："+homePageId);
        }

        String str=sql.toString();
        CustomPlatformCountInfo cpc = sqlMapper.selectOne(str,CustomPlatformCountInfo.class);
     //   log.info("--------"+cpc+"   cpc.getVisitCount: "+cpc.getVisitCount());
        return cpc;
    }

    /*
     * 插入新的一天的第一条数据
     */
    @Override
    public int insertCustomPlatformCountInfo(String platformId,String buttonId,String starPlatformId,String bannerId,String homePageId, Date date,Integer stuts) {
        SimpleDateFormat sdfCreate=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//
        SimpleDateFormat sdfUpdate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String creatDay=sdfCreate.format(date);//创建时间
        String sql = null;
       // log.info("创建时间："+creatDay);
        String updateDay=sdfUpdate.format(date);//修改时间
      //  log.info("创建时间："+updateDay);
        int id = 0;
        try {
            if (stuts == 0){
                sql="insert into customer_platform_count_info ";
                if (StringUtil.isNotEmpty(platformId)){
                    sql += "(platform_id,visit_count,creat_time,update_time) values ('"+platformId+"'"+",'"+1+"'"+",'"+
                            creatDay+"','"+updateDay+"') ";
                }
                if (StringUtil.isNotEmpty(buttonId)){
                    sql += "(button_id,visit_count,creat_time,update_time) values ('"+buttonId+"'"+",'"+1+"'"+",'"+
                            creatDay+"','"+updateDay+"') ";
                }
                if (StringUtil.isNotEmpty(starPlatformId)){
                    sql += "(star_platform_id,visit_count,creat_time,update_time) values ('"+starPlatformId+"'"+",'"+1+"'"+",'"+
                            creatDay+"','"+updateDay+"') ";
                }
                if (StringUtil.isNotEmpty(bannerId)){
                    sql += "(banner_id,visit_count,creat_time,update_time) values ('"+bannerId+"'"+",'"+1+"'"+",'"+
                            creatDay+"','"+updateDay+"') ";
                }
                if (StringUtil.isNotEmpty(homePageId)){
                    sql += "(home_page_id,visit_count,creat_time,update_time) values ('"+homePageId+"'"+",'"+1+"'"+",'"+
                            creatDay+"','"+updateDay+"') ";
                }
                id = sqlMapper.insert(sql);
            }else {
                sql="insert into customer_platform_count_info ";
                if (StringUtil.isNotEmpty(platformId)){
                    sql += "(platform_id,click_count,creat_time,update_time) values ('"+platformId+"'"+",'"+1+"'"+",'"+
                            creatDay+"','"+updateDay+"') ";
                }
                id = sqlMapper.insert(sql);
            }

        } catch (Exception e) {
            // TODO Auto-generated catch block
      //      log.error("插入数据异常", e);
        }
        return id;
    }

    /*
     * 更新访问量
     */
    @Override
    public int updateCount(String platformId,String buttonId,String starPlatformId,String bannerId,String homePageId ,Date date,Integer stuts) {
       CustomPlatformCountInfo cpc = selectCustomPlatformCountInfo(platformId,buttonId,starPlatformId,bannerId,homePageId,date);
       long visit = 0;
       String sql = null;
       int updataCount = 0;
       if (stuts == 0){
    //       log.info("visitCount:"+cpc.getVisitCount());
           if (cpc.getVisitCount() == null){
               visit = 1;
           }else {
               visit = cpc.getVisitCount() + 1;
           }
           sql = ("update customer_platform_count_info set visit_count=" + visit + " where id=" + cpc.getId());
           updataCount = sqlMapper.update(sql);
       }else {
           if (cpc.getClickCount() == null){
               visit = 1;
           }else {
               visit = cpc.getClickCount() + 1;
           }
            sql =("update customer_platform_count_info set click_Count="+visit+" where id="+cpc.getId());
            updataCount=sqlMapper.update(sql);
       }

        return updataCount;
    }
}
